package com.ww.springboot.mybatis.study.jdbc;

import cn.hutool.json.JSONUtil;

import java.sql.*;

public class JDBCDemo {
    public static void main(String[] args) {
        try {
            // 加载数据库驱动程序
            Class.forName("com.mysql.cj.jdbc.Driver");
            // 连接数据库
            Connection conn = DriverManager.getConnection(
                    "jdbc:mysql://10.10.40.56:3306/cpms", "cpms", "nYLM5LF0nVuS8CFInWj6cg");
            // 创建一个Statement对象
            //statementDemo(conn);
            //prepareStatement(conn);
            metaPrint(conn);
            conn.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static void metaPrint(Connection conn) throws SQLException {
        DatabaseMetaData metaData = conn.getMetaData();
        System.out.println(metaData.getURL());
        System.out.println(metaData.getUserName());
        System.out.println(metaData.getDatabaseProductName());
        System.out.println(metaData.getDriverName());
        System.out.println(metaData.getSearchStringEscape());
        System.out.println(metaData.getDriverVersion());
        System.out.println(metaData.getJDBCMajorVersion());

        System.out.println("------------------------------------------");

        System.out.println(metaData.getMaxConnections());
        System.out.println(metaData.getMaxRowSize());
        System.out.println(metaData.getMaxTablesInSelect());
        System.out.println(metaData.getMaxColumnsInTable());


        System.out.println("------------------------------------------");
        System.out.println(metaData.getDefaultTransactionIsolation());

        System.out.println("-------------------------------------------");
        ResultSet tables = metaData.getTables("cpms", null, null, null);
        while (tables.next()) {
            System.out.println(tables.getString("TABLE_NAME"));
        }
        System.out.println("-------------------------------------------");
        ResultSet catalogs = metaData.getCatalogs();
        while (catalogs.next()) {
            System.out.println(catalogs.getString("TABLE_CAT"));
        }
        System.out.println("-------------------------------------------");
        ResultSet schemas = metaData.getSchemas();
        while (schemas.next()) {
            System.out.println(schemas.getString("TABLE_SCHEM"));
        }
    }

    private static void prepareStatement(Connection conn) throws SQLException {
        PreparedStatement stmt = conn.prepareStatement("SELECT * FROM pms_prop where id = ?");
        stmt.setString(1, "2");
        ResultSet rs = stmt.executeQuery();
        // 处理查询结果集
        while (rs.next()) {
            System.out.println(rs.getLong("id") + " "
                    + rs.getString("prop_key") + " " + rs.getString("prop_value"));
        }
        // 关闭查询结果集、Statement对象和数据库连接
        rs.close();
        stmt.close();
    }

    private static void statementDemo(Connection conn) throws SQLException {
        Statement stmt = conn.createStatement();
        // 执行SQL查询
        ResultSet rs = stmt.executeQuery("SELECT * FROM pms_prop");
        // 处理查询结果集
        while (rs.next()) {
            System.out.println(rs.getLong("id") + " "
                    + rs.getString("prop_key") + " " + rs.getString("prop_value"));
        }
        // 关闭查询结果集、Statement对象和数据库连接
        rs.close();
        stmt.close();
    }
}
